<template>
	<view class="page">
      <u-empty v-if="carList.length == 0" mode="car" text-color="#404040" text="购物车为空" ></u-empty>
      <u-swipe-action >
        <u-swipe-action-item  :options="options1"  @click="click" v-for="item in carList" :name="item.id" :key="item.id" :index="item.id">
            <view carBox-item style="border: 1px solid #4d4640;border-radius: 20px;margin: 10px 10px;" >
              <img style="width: 150px;height:200px;float: left;border-radius: 20px;" :src="item.img"  alt="" />
						  <view>
                <view style="margin-top: 3px;" @click="toProductDetail(item)">{{item.name}}</view>
							  <view style="margin-top: 3px;">单价：{{ item.price }}积分</view>
							  <u-number-box style="margin-top: 3px;" v-model="item.count"  input-width="20" button-size="20" bg-color="#787878" icon-style="color: #fff"></u-number-box>
                <view style="margin-top: 3px;">小计:{{ item.price*item.count }}积分 </view>
                <button style="width: 100px;border-radius: 20px;" @click="gobuy(item)">去支付</button>
						  </view>
            </view>
        </u-swipe-action-item>
      </u-swipe-action>
	</view>
</template>

<script>
import {baseUrl} from '@/config'
export default {
	data() {
		return {
     
      userId:0,
      userInfo:[],
      id:0,
      carList:[],
      shopOrder:[],
      options1: [{
                    text: '删除',
                    style: {
                        backgroundColor: '#787878'
                    }
                }
                
              ]
		};
	},
	onShow() {
	this.getCarList()
	},
	methods: {
    gobuy(item){
      let id  = item.id
      let count = item.count
      let path = 'car'
      uni.showToast({
          title: '跳转支付页面',
          duration: 1000
      });
        setTimeout(function() {
            uni.navigateTo({
              url:`/pages/shop-confirm/index?id=${id}&count=${count}&path=${path}`
          })
				}, 1500)
    },
    click({ name,index}) {
    this.id = name
			switch (index) {
				case 0:
					uni.showModal({
						title: '提示',
						content: '是否删除？',
						success:  res => {
							if (res.confirm) {
                uni.request({
                        url: baseUrl+'/v1/car/carDelete', 
                        data:{
                          id:this.id
                        },
                        success: () => {
                          
                          this.getCarList()
                         
                          
                        } 
                      });
							}
						}
					});
					break;
			}
		},
    toProductDetail(item){
        uni.navigateTo({
           url: `/pages/product-detail/index?id=${item.id}`
        });
    },
    getCarList(){
      this.userId=uni.getStorageSync('user').data[0].userId
      uni.request({
          url: baseUrl+'/v1/car/allSearch', 
          data:{
            userId:this.userId
          },
          success:(res)=>{
            if(res.data.code == 200){
              this.carList = res.data.data    
            }else{
              this.carList = []
            }
             
          }
          
        })
    },
	}
};
</script>

<style lang="scss" scoped>

</style>
